<?php
/**
 * @author Feng
 * @date 2012-5-28
 */
class controller_admin_collection_item extends system_user_control {
	
	private $concept = null;
	private $site = null;
	public function init() {
		if(!parent::init()) {
			return ;
		}
		picture_category::$_lang = $this->user->getLang();
		$site_id = intval($this->_param('site_id'));
		$concept = new collection_site_concept($this->user->getLang());
		if(!($this->site = $concept->fetch(array('site.id = ?' => $site_id)))) {
			throw new collection_exception("The Site is not Existing.");
		}
		$this->view->site = $this->site;
		$this->concept = new collection_item_concept(array('site_id' => $site_id), array('site_id = ?' => $site_id));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Collections'))->setRoute('collection/index'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Sites'))->setRoute('collection/site/index'));
		$this->_crumbs()->addPage(system_nav_page::create($this->site->getTitle())->setRoute('collection/site/edit')->setParams(array('id' => $site_id)));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Items'))->setRoute('collection/item/index')->setParams(array('site_id' => $site_id)));
	}
	
	public function action_collect() {
		$concept = new collection_item_concept();
		$id = intval($this->_param('id'));
		if(!($row = $concept->fetch(array('item.id = ?' => $id)))) {
			throw new collection_exception("The Item is not Existing.");
		}

		$categoryList = array();
		$concept = new collection_item_category_concept();
		foreach($concept->fetchAll(array('item_id = ?' => $id)) AS $_row) {
			$categoryList[] = $_row->getCategoryId();
		}
		$this->view->categoryList = $categoryList;
		$this->view->row = $row;
		$this->_crumbs()->addPage(system_nav_page::create($row->getTitle())->setRoute('collection/item/collect')->setParams(array('id' => $row->getId(), 'site_id' => $this->site->getId())));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Collect Special'))->setRoute('collection/site/collect')->setParams(array('id' => $row->getId(), 'site_id' => $this->site->getId())));
		$this->render();
	}
	
	public function action_addnew() {
		$id = intval($this->_param('id'));
		$row = $this->concept->create();
		$row->setSiteId($this->site->getId());
		$row->setRule((string)$this->site->getItemRule());
		$row->setRuleId($this->site->getItemRuleId());
		$row->setType('item');
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('New Item'))->setRoute('collection/site/addnew')->setParams(array('site_id' => $this->site->getId())));
		$this->render('collection/item/row.phtml');
	}
	
	public function action_edit() {
		$id = intval($this->_param('id'));
		if(!($row = $this->concept->fetch(array('item.id = ?' => $id)))) {
			throw new collection_exception("The Item is not Existing.");
		}
		$categoryList = array();
		$concept = new collection_item_category_concept();
		foreach($concept->fetchAll(array('item_id = ?' => $id)) AS $_row) {
			$categoryList[] = $_row->getCategoryId();
		}
		$this->view->categoryList = $categoryList;
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($row->getTitle())->setRoute('collection/site/edit')->setParams(array('id' => $row->getId(), 'site_id' => $this->site->getId())));
		$this->render('collection/item/row.phtml');
	}
	
	private function _saveRow($row) {
		if(!$this->getRequest()->isPost()) {
			return $row;
		}
		$this->referer = $this->_post('referer');
		$this->view->categoryList = $categoryList = (array) $this->_post('category_id');
		$rule = $this->_post('rule');
		$newRule = $this->_post('newRule');
		if(isset($rule['item'])) {
			$row->setRule($rule['item']);
		}	
		if(isset($newRule['item'])) {
			$row->setRuleId(0);
		}
		foreach($categoryList AS $cid) {
			$row->addCategoryList($cid);
		}
		$row->setTitle(trim($this->_post('title')));
		$row->setUrl(trim($this->_post('url')));
		$this->_saveToDatabase($row, 'cover', 'image');		
		return $row;
	}
	
	public function action_list() {	
		$adapter = new system_paginator_adapter_concept($this->concept);
		$adapter->setCurrentPageNumber(intval($this->getRequest()->getParam('page', 1)));
		$adapter->setLimit(30);
		$adapter->order(array("item.id DESC"));
		$this->view->list = new system_paginator($adapter);;

		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('collection/item/list')->setParams(array('site_id' => $this->site->getId())));
		$this->render();
	}
	
	public function action_index() {
		$this->_goRoute('collection/item/list', array('site_id' => $this->site->getId()));
		$this->render();
	}
}
